package controller;

import com.alibaba.druid.util.StringUtils;
import com.shop.entity.Result;
import com.shop.pojo.Product;
import com.shop.pojo.User;
import com.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;


    /** 1.发布商品 */
    @RequestMapping("/addProduct.do")
    @ResponseBody
    public Result addProduct(Product pro , HttpServletRequest request) {
        Result result = null;
        try{
            User user = (User)request.getSession().getAttribute("user");
            if (user == null || StringUtils.isEmpty(user.getId())){
                result = new Result(false,"请先登录");
                return result;
            }
            pro.setbUid(user.getId());
            result = productService.addProduct(pro);
        }catch (Exception e){
            result = new Result(false,e.getMessage());
        }
        return result;
    }

    /** 2.查询指定商品详情 （根据书的id） */
    @RequestMapping("/findProductById.do")
    @ResponseBody
    public Product findProductById(String id) {
        Product pro = null;
        try{
            pro = productService.findProductById(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return pro;
    }

    /** 3.查询所有商品  (正在出售的) */
    @RequestMapping("/findAllProducts.do")
    @ResponseBody
    public List<Product> findAllProducts() {
        return null;
    }

    /** 4.删除商品 */
    @RequestMapping("/findLatestProducts.do")
    @ResponseBody
    public List<Product> findLatestProducts(String pid) {
        return null;
    }

    /** 5.修改商品 */
    @RequestMapping("/updateProduct.do")
    @ResponseBody
    public Result updateProduct(Product product) {
        return null;
    }


}
